﻿using System.Collections.Generic;
using System.Reflection;
using Zhp.Common.Appsetting;
using Zhp.Common.Extention;
using Zhp.Common.Helper;
using Zhp.IService.Common;
using Zhp.Model.Common;

namespace Zhp.Service.Common
{
    public class CommonService : ICommonService
    {
        #region 系统枚举下拉框

        /// <summary>
        /// 参数传递枚举类型字符串
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public async Task<List<EnumModel>> GetEnumList(string enumType)
        {
            var list = new List<EnumModel>();

            var program = AppSettingHelper.GetOptions<ProgramCodeOptions>();
            var dll = Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory, $"{program.EntityName}.dll"));
            var type = dll.GetType($"Zhp.Entity.SysEnum.{enumType}");

            if (type == null)
            {
                return list;
            }

            var fields = type.GetEnumFields();
            foreach (var field in fields)
            {
                list.Add(new EnumModel
                {
                    Name = field.Name,
                    Value = field.GetValue(type).GetHashCode(),
                    Text = field.GetDescriptionFirst()
                });
            }

            return await Task.FromResult(list);
        }

        #endregion

        public async Task<object> GetLongId(int? num)
        {
            if (num.HasValue)
            {
                var list = new List<long>();
                for (int i = 0; i < num; i++)
                {
                    list.Add(SnowFlakeHelper.GetLongId());
                }
                return await Task.FromResult(list);
            }
            else
            {
                return await Task.FromResult(SnowFlakeHelper.GetLongId());
            }
        }
    }
}
